--- ========================================================= ---
---  Setup Revision, Version, Date and add to AceLibrary
--- ========================================================= ---
local VERSION = tonumber(("$Revision: 52619 $"):match("%d+"))

local DaHud = DaHud
DaHud_Module_Bars = DaHud:NewModule("Module_Bars", "AceEvent-2.0")
local self = DaHud_Bars
if DaHud.revision < VERSION then
	DaHud.version = "r" .. VERSION
	DaHud.revision = VERSION
	DaHud.date = ("$Date: 2007-10-19 21:07:04 -0400 (Fri, 19 Oct 2007) $"):match("%d%d%d%d%-%d%d%-%d%d")
end
DaHud.desc = "Bars Module"

--- ========================================================= ---
---  Setup Locals
--- ========================================================= ---
local DogTag = AceLibrary("DogTag-1.0")

local L = DaHud:L("DaHud_Module_Bars")

local starttime = {
}

local eventType = {
	health = "[PercentHP:Div(100)]",
	power = "[PercentMP:Div(100)]",
	threat = "[PercentThreat:Div(100)]",
	experience = "[PercentXP:Div(100)]",
	reputation = "[PercentRep:Div(100)]",
	none = "",
	custom = "",
}

--- ========================================================= ---
---  On Intitalize / Setup Database
--- ========================================================= ---
function DaHud_Module_Bars:OnInitialize()
	self.db = DaHud:AcquireDBNamespace("Module_Bars")
	DaHud:RegisterDefaults("Module_Bars", "profile", {
		number = 10,
		hudframe1 = {"Player Health", true, true, "left", "LOW", 6, 120, 260, 1, -160, 0, "BOTTOM", "DaHudFrame1", "BOTTOM", "player", "health", "[PercentHP:Div(100)]", true, true},
		hudframe2 = {"Player Mana", true, true, "right", "LOW", 6, 120, 260, 1, -140, 0, "BOTTOM", "DaHudFrame2", "BOTTOM", "player", "power", "[PercentMP:Div(100)]", true, true},
		hudframe3 = {"Target Health", true, true, "right", "LOW", 6, 120, 260, 1, 160, 0, "BOTTOM", "DaHudFrame3", "BOTTOM", "target", "health", "[PercentHP:Div(100)]", true, true},
		hudframe4 = {"Target Mana", true, true, "left", "LOW", 6, 120, 260, 1, 140, 0, "BOTTOM", "DaHudFrame4", "BOTTOM", "target", "power", "[PercentMP:Div(100)]", true, true},
		hudframe5 = {"Pet Health", true, true, "center", "LOW", 4, 160, 260, 0.50, -240, -123, "BOTTOM", "DaHudFrame5", "BOTTOM", "pet", "health", "[PercentHP:Div(100)]", true, true},
		hudframe6 = {"Pet Mana", true, true, "right", "LOW", 4, 160, 260, 0.50, -220, -123, "BOTTOM", "DaHudFrame6", "BOTTOM", "pet", "power", "[PercentMP:Div(100)]", true, true},
		hudframe7 = {"Target of Target Health", true, true, "center", "LOW", 4, 160, 260, 0.50, 240, -123, "BOTTOM", "DaHudFrame7", "BOTTOM", "targettarget", "health", "[PercentHP:Div(100)]", false, true},
		hudframe8 = {"Target of Target Mana", true, true, "left", "LOW", 4, 160, 260, 0.50, 220, -123, "BOTTOM", "DaHudFrame8", "BOTTOM", "targettarget", "power", "[PercentMP:Div(100)]", false, true},
		hudframe9 = {"Player Experience", true, true, "double", "LOW", 6, 120, 260, 1, -210, 0, "BOTTOM", "DaHudFrame9", "BOTTOM", "player", "experience", "[PercentXP:Div(100)]", true, false},
		hudframe10 = {"Focus Health", true, true, "double", "LOW", 6, 120, 260, 1, 210, 0, "BOTTOM", "DaHudFrame10", "BOTTOM", "focus", "health", "[PercentHP:Div(100)]", false, true},
	})
end

--- ========================================================= ---
---  On Enable
--- ========================================================= ---
function DaHud_Module_Bars:OnEnable()
	for i=1, self.db.profile.number do
		starttime["hudframe"..i] = nil
	end

	self:SetupOptions()
	self:CreateFrames()
	self:UpdateAllFrames()

	self:RegisterEvent("DaHud_SetConfigMode")
	self:RegisterEvent("DaHud_ReloadAllFrames")
	
	self:RegisterEvent("PLAYER_TARGET_CHANGED")

	self:ScheduleRepeatingEvent("DaHud_Bar_ProcessAllFrames", self.ProcessAllFrames, 0.05, self)
end

--- ========================================================= ---
---  On Disable
--- ========================================================= ---
function DaHud_Module_Bars:OnDisable()
	self:UnregisterAllEvents();
end

--- ========================================================= ---
---  DaHud_SetConfigMode
--- ========================================================= ---
function DaHud_Module_Bars:DaHud_SetConfigMode()
	self:UpdateAllFrames()
end

--- ========================================================= ---
---  DaHud_ReloadAllFrames
--- ========================================================= ---
function DaHud_Module_Bars:DaHud_ReloadAllFrames()
	self:SetupOptions()
	self:UpdateAllFrames()
end

--- ========================================================= ---
---  PLAYER_TARGET_CHANGED
--- ========================================================= ---
function DaHud_Module_Bars:PLAYER_TARGET_CHANGED()
	for i=1, self.db.profile.number do
		local frame = getglobal("DaHudFrame"..i)
		local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(DaHud.db.namespaces.Module_Bars.profile["hudframe"..i])
		if unit == "target" then
			frame.targetchanged = true
		end
	end
end

--- ========================================================= ---
---  Setup Validates for Options
--- ========================================================= ---
local strataValidate = {
	BACKGROUND = L["Background"],
	LOW = L["Low"],
	MEDIUM = L["Medium"],
	HIGH = L["High"],
	DIALOG = L["Dialog"],
	TOOLTIP = L["Tooltip"],
}

local strataValidateDesc = {}

local frameTexValidate = {
	left = L["Left"],
	center = L["Center"],
	right = L["Right"],
	double = L["Double"],
}

local frameTexValidateDesc = {}

local eventTypeValidate = {
	health = L["Health"],
	power = L["Power"],
	threat = L["Threat"],
	experience = L["Experience"],
	reputation = L["Reputation"],
	custom = L["Custom"],
	none = L["None"],
}

local eventTypeValidateDesc = {}

local eventUnitValidate = {
	player = L["Player"],
	target = L["Target"],
	focus = L["Focus"],
	pet = L["Pet"],
	targettarget = L["Target of Target"],
	targettargettarget = L["ToT of Target"],
}

local eventUnitValidateDesc = {}

--- ========================================================= ---
---  Setup Options
--- ========================================================= ---
function DaHud_Module_Bars:SetupOptions()
	DaHud.options.args.barframes = {
		name = L["Bar Frames"],
		desc = L["Change the Bar Frame options."],
		type = 'group',
		args = {
		},
	}

	for i=1, self.db.profile.number do
		local frame = "hudframe"..i
			
		self:UpdateMainOption(frame)
		self:UpdateNameOption(frame)
		self:UpdateShowOption(frame)
		self:UpdateBGOption(frame)
		self:UpdateTextureOption(frame)
		self:UpdateFramestrataOption(frame)
		self:UpdateFramelevelOption(frame)
		self:UpdateWidthOption(frame)
		self:UpdateHeightOption(frame)
		self:UpdateScaleOption(frame)
		self:UpdatePosXOption(frame)
		self:UpdatePosYOption(frame)
		self:UpdateUnitOption(frame)
		self:UpdateTypeOption(frame)
		self:UpdateCustomOption(frame)
		self:UpdateAnimateOption(frame)
		self:UpdateCastingbarOption(frame)
	end
end

--- ========================================================= ---
---  Update Main Option
--- ========================================================= ---
function DaHud_Module_Bars:UpdateMainOption(frame)
	local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])

	DaHud.options.args.barframes.args[frame] = {
		name = name,
		desc = L["Change the Text Frame options."],
		type = 'group',
		args = {
		}
	}
end

--- ========================================================= ---
---  Update Name Option
--- ========================================================= ---
function DaHud_Module_Bars:UpdateNameOption(frame)
	DaHud.options.args.barframes.args[frame].args.name = {
		name = L["Name"],
		desc = L["Name the Frame."],
		type = 'text',
		usage = "<name>",
		get = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			return name
		end,
		set = function(value) 
			self:SaveOptions(frame, "name", value)
			self:SetupOptions()
		end,
	}
end

--- ========================================================= ---
---  Update Show Option
--- ========================================================= ---
function DaHud_Module_Bars:UpdateShowOption(frame)
	DaHud.options.args.barframes.args[frame].args.show = {
		name = L["Show"],
		desc = L["Show this Frame."],
		type = 'toggle',
		get = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			return show
		end,
		set = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			self:SaveOptions(frame, "show", not(show))
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update BG Option
--- ========================================================= ---
function DaHud_Module_Bars:UpdateBGOption(frame)
	DaHud.options.args.barframes.args[frame].args.bg = {
		name = L["Background Bar"],
		desc = L["Show the Background Bar."],
		type = 'toggle',
		get = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			return bg
		end,
		set = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			self:SaveOptions(frame, "bg", not(bg))
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Texture Option
--- ========================================================= ---
function DaHud_Module_Bars:UpdateTextureOption(frame)
	DaHud.options.args.barframes.args[frame].args.tex = {
		name = L["Frame Texture"],
		desc = L["Choose which Frame Texture to use."],
		type = 'text',
		validate = frameTexValidate,
		validateDesc = frameTexValidateDesc,
		get = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			return tex
		end,
		set = function(value)
			self:SaveOptions(frame, "tex", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Framestrata Option
--- ========================================================= ---
function DaHud_Module_Bars:UpdateFramestrataOption(frame)
	DaHud.options.args.barframes.args[frame].args.framestrata = {
		name = L["Frame Strata"],
		desc = L["Choose the layer the portrait is drawn on."],
		type = 'text',
		validate = strataValidate,
		validateDesc = strataValidateDesc,
		get = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			return framestrata
		end,
		set = function(value) 
			self:SaveOptions(frame, "framestrata", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Framelevel Option
--- ========================================================= ---
function DaHud_Module_Bars:UpdateFramelevelOption(frame)
	DaHud.options.args.barframes.args[frame].args.framelevel = {
		name = L["Frame Level"],
		desc = L["Change the Frame Level it is drawn on."],
		type = 'range',
		min = 1,
		max = 12,
		step = 1,
		bigStep = 2,
		get = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			return framelevel
		end,
		set = function(value) 
			self:SaveOptions(frame, "framelevel", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Width Option
--- ========================================================= ---
function DaHud_Module_Bars:UpdateWidthOption(frame)
	DaHud.options.args.barframes.args[frame].args.width = {
		name = L["Width"],
		desc = L["Change the Width."],
		type = 'range',
		min = 0,
		max = 500,
		step = 1,
		bigStep = 20,
		get = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			return width
		end,
		set = function(value) 
			self:SaveOptions(frame, "width", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Height Option
--- ========================================================= ---
function DaHud_Module_Bars:UpdateHeightOption(frame)
	DaHud.options.args.barframes.args[frame].args.height = {
		name = L["Height"],
		desc = L["Change the Height."],
		type = 'range',
		min = 0,
		max = 500,
		step = 1,
		bigStep = 20,
		get = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			return height
		end,
		set = function(value) 
			self:SaveOptions(frame, "height", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Scale Option
--- ========================================================= ---
function DaHud_Module_Bars:UpdateScaleOption(frame)
	DaHud.options.args.barframes.args[frame].args.scale = {
		name = L["Scale"],
		desc = L["Change the Scale."],
		type = 'range',
		min = 0.25,
		max = 3,
		step = 0.01,
		bigStep = 0.05,
		get = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			return scale
		end,
		set = function(value) 
			self:SaveOptions(frame, "scale", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update POS X Option
--- ========================================================= ---
function DaHud_Module_Bars:UpdatePosXOption(frame)
	DaHud.options.args.barframes.args[frame].args.posx = {
		name = L["Pos X"],
		desc = L["Change the X Position."],
		type = 'range',
		min = -500,
		max = 500,
		step = 1,
		bigStep = 20,
		get = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			return posx
		end,
		set = function(value) 
			self:SaveOptions(frame, "posx", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update POS Y Option
--- ========================================================= ---
function DaHud_Module_Bars:UpdatePosYOption(frame)
	DaHud.options.args.barframes.args[frame].args.posy = {
		name = L["Pos Y"],
		desc = L["Change the Y Position."],
		type = 'range',
		min = -500,
		max = 500,
		step = 1,
		bigStep = 20,
		get = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			return posy
		end,
		set = function(value) 
			self:SaveOptions(frame, "posy", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Unit Option
--- ========================================================= ---
function DaHud_Module_Bars:UpdateUnitOption(frame)
	DaHud.options.args.barframes.args[frame].args.unit = {
		name = L["Event Unit"],
		desc = L["Choose which Unit the Event is monitoring and showing."],
		type = 'text',
		validate = eventUnitValidate,
		validateDesc = eventUnitValidateDesc,
		get = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			return unit
		end,
		set = function(value) 
			self:SaveOptions(frame, "unit", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Type Option
--- ========================================================= ---
function DaHud_Module_Bars:UpdateTypeOption(frame)
	DaHud.options.args.barframes.args[frame].args.type = {
		name = L["Event Type"],
		desc = L["Choose which Event Type to show."],
		type = 'text',
		validate = eventTypeValidate,
		validateDesc = eventTypeValidateDesc,
		get = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			return type
		end,
		set = function(value)
			local style = eventType[value]
			self:SaveOptions(frame, "type", value)
			self:SaveOptions(frame, "custom", style)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Custom Option
--- ========================================================= ---
function DaHud_Module_Bars:UpdateCustomOption(frame)
	DaHud.options.args.barframes.args[frame].args.custom = {
		name = L["Custom Event"],
		desc = L["Create a Custom Event for the Gauge to monitor and show."],
		type = "text",
		usage = L["DogTag-1.0 tags. See http://www.wowace.com/wiki/DogTag-1.0"],
		get = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			return custom
		end,
		set = function(value) 
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			if type == "custom" then
				self:SaveOptions(frame, "custom", value)
				self:UpdateAllFrames()
			end
		end,
	}
end

--- ========================================================= ---
---  Update Animate Option
--- ========================================================= ---
function DaHud_Module_Bars:UpdateAnimateOption(frame)
	DaHud.options.args.barframes.args[frame].args.animate = {
		name = L["Animate"],
		desc = L["Animate (Glide)."],
		type = 'toggle',
		get = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			return animate
		end,
		set = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			self:SaveOptions(frame, "animate", not(animate))
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Castingbar Option
--- ========================================================= ---
function DaHud_Module_Bars:UpdateCastingbarOption(frame)
	DaHud.options.args.barframes.args[frame].args.castingbar = {
		name = L["Casting Bar"],
		desc = L["Show Casting Bar."],
		type = 'toggle',
		get = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			return castingbar
		end,
		set = function()
			local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
			self:SaveOptions(frame, "castingbar", not(castingbar))
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Save Options
--- ========================================================= ---
function DaHud_Module_Bars:SaveOptions(frame, var, value)
	local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[frame])
	
	if var == "name" then
		name = value
	elseif var == "show" then
		show = value
	elseif var == "bg" then
		bg = value
	elseif var == "tex" then
		tex = value
	elseif var == "framestrata" then
		framestrata = value
	elseif var == "framelevel" then
		framelevel = value
	elseif var == "width" then
		width = value
	elseif var == "height" then
		height = value
	elseif var == "scale" then
		scale = value
	elseif var == "align" then
		align = value
	elseif var == "posx" then
		posx = value
	elseif var == "posy" then
		posy = value
	elseif var == "unit" then
		unit = value
	elseif var == "type" then
		type = value
	elseif var == "style" then
		style = value
	elseif var == "custom" then
		custom = value
	elseif var == "animate" then
		animate = value
	elseif var == "castingbar" then
		castingbar = value
	end
	
	self.db.profile[frame] = {name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar}
end	

--- ========================================================= ---
---  Create Frame
--- ========================================================= ---
function DaHud_Module_Bars:CreateFrame(frametype, framename, parent, vals)
	local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[vals])
	local f = CreateFrame(frametype, framename, parent)

	f:SetWidth(width)
	f:SetHeight(height)
	f:SetScale(scale)
	f:SetPoint("Center", parent, "Center", posx, posy)
	f:SetFrameStrata(framestrata)
	f:SetFrameLevel(framelevel)
	
	f.unit = unit
	f.framename = framename
	
	f:SetAttribute("unit", unit)
	f:SetAttribute("shown", "unit")
	f:SetAttribute("useunit", unit)
	RegisterUnitWatch(f)
	
	local tbg = f:CreateTexture(framename.."TextureBG", "BACKGROUND")
	tbg:SetTexture("Interface\\Addons\\DaHud\\Art\\Bar")
	local r, g, b, a = unpack(DaHud.db.profile.colors.barbg)
	tbg:SetVertexColor(r, g, b, a)
	tbg:SetAllPoints(f)
	
	local t = f:CreateTexture(framename.."Texture", "ARTWORK")
	t:SetTexture("Interface\\Addons\\DaHud\\Art\\CenterHud")
	t:SetAllPoints(f)

	f:Show()

	return f
end

--- ========================================================= ---
---  Create Bar
--- ========================================================= ---
function DaHud_Module_Bars:CreateBar(texname, parent, layer, framename, vals)
	local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[vals])
	local gap_top, gap_bottom, gap_left, gap_right = 9, 9, 0, 0
	local t = parent:CreateTexture(texname, framestrata)

	t:SetTexture("Interface\\Addons\\DaHud\\Art\\Bar")

	local p = 1
	local gap_top_p = gap_top / height;    
	local gap_bottom_p = gap_bottom / height;
	local h = (height - gap_top - gap_bottom) * p;
	local top = 1-(p-(gap_top_p));
	local bottom = 1-gap_bottom_p;
	local top = top - ((gap_top_p+gap_bottom_p)*(1-p));
	t:SetHeight(h);
	t:SetWidth(width);
	t:SetTexCoord(0, 1, top, bottom );
	t:SetPoint(point, framename, relative, gap_left, gap_bottom);

	t:Show()

	return t
end

--- ========================================================= ---
---  Create Frames
--- ========================================================= ---
function DaHud_Module_Bars:CreateFrames()
	for i=1, self.db.profile.number do
		self:CreateFrame("Frame", "DaHudFrame"..i, getglobal("DaHudFrame"), "hudframe"..i)
		self:CreateBar("DaHudFrame"..i.."Bar", getglobal("DaHudFrame"..i), "OVERLAY", "DaHudFrame"..i, "hudframe"..i)
	end
end

--- ========================================================= ---
---  Update Frames
--- ========================================================= ---
function DaHud_Module_Bars:UpdateAllFrames()
	for i=1, self.db.profile.number do
		self:UpdateFrame("DaHudFrame"..i, "hudframe"..i)
	end
end

--- ========================================================= ---
---  Update Frame
--- ========================================================= ---
function DaHud_Module_Bars:UpdateFrame(framename, vals)
	local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[vals])
	local f = getglobal(framename)
	local texturebg = getglobal(framename.."TextureBG")
	local texture = getglobal(framename.."Texture")
	
	if DaHud_ConfigMode then
		unit = "player"
	end
	
	f:SetWidth(width)
	f:SetHeight(height)
	f:SetScale(scale)
	f:SetPoint("Center", getglobal("DaHudFrame"), "Center", posx, posy)
	f:SetFrameStrata(framestrata)
	f:SetFrameLevel(framelevel)
	
	f.unit = unit
	f.framename = framename
	
	f:SetAttribute("unit", unit)
	f:SetAttribute("shown", "unit")
	f:SetAttribute("useunit", unit)
	
	local r, g, b, a = unpack(DaHud.db.profile.colors.barbg)
	texturebg:SetVertexColor(r, g, b, a)
	texturebg:SetAllPoints(f)
	
	if tex == "left" then
		texture:SetTexture("Interface\\Addons\\DaHud\\Art\\LeftHud")
	elseif tex == "right" then
		texture:SetTexture("Interface\\Addons\\DaHud\\Art\\RightHud")
	elseif tex == "center" then
		texture:SetTexture("Interface\\Addons\\DaHud\\Art\\CenterHud")
	elseif tex == "double" then
		texture:SetTexture("Interface\\Addons\\DaHud\\Art\\DoubleHud")
	end
	
	if bg then
		texturebg:Show()
	else
		texturebg:Hide()
	end	
	
	if show then
		RegisterUnitWatch(f)
	else
		UnregisterUnitWatch(f)
		f:Hide()
	end	
end

--- ========================================================= ---
---  Process All Frames
--- ========================================================= ---
function DaHud_Module_Bars:ProcessAllFrames()
	for i=1, self.db.profile.number do
		self:ProcessFrame("DaHudFrame"..i, "hudframe"..i)
	end
end

--- ========================================================= ---
---  Process Frame
--- ========================================================= ---
function DaHud_Module_Bars:ProcessFrame(framename, vals)
	local name, show, bg, tex, framestrata, framelevel, width, height, scale, posx, posy, point, anchor, relative, unit, type, custom, animate, castingbar = unpack(self.db.profile[vals])
	local gap_top, gap_bottom, gap_left, gap_right = 9, 9, 0, 0
	local r, g, b, a, p
	local animatespeed = DaHud.db.profile.settings.animatespeed
	local start = starttime[vals]
	local frame = getglobal(framename)
	local texture = getglobal(framename.."Texture")
	local bar = getglobal(framename.."Bar")
	
	if DaHud_ConfigMode then
		unit = "player"
	end
	
	if tonumber(DogTag:Evaluate(unit, custom)) then
		p = tonumber(DogTag:Evaluate(unit, custom))
	else
		p = 0
	end
	
	if type == "power" then
		local powerType = DogTag:Evaluate(unit, "[TypePower]")
		if powerType == "" then powerType = "Mana" end
		r, g, b, a = unpack(DaHud.db.profile.colors[string.lower(powerType)])
	else
		r, g, b, a = unpack(DaHud.db.profile.colors[type])
	end
	
	if animate and not(frame.targetchanged) then
		if (start == nil) then
			start = p;
		end
		if (start > p) then
			start = start - animatespeed;
			if (start < p) then
				start = p;
			end
		else
			start = start + animatespeed;
			if (start > p) then
				start = p;
			end
		end
		p = start
		starttime[vals] = start
	end

	if (p > 1) then
		p = 1;
	end
	
	local gap_top_p    = gap_top / height;    
	local gap_bottom_p = gap_bottom / height;
	local h = (height - gap_top - gap_bottom) * p;
	local top    = 1-(p-(gap_top_p));
	local bottom = 1-gap_bottom_p;
	local top = top  - ((gap_top_p+gap_bottom_p)*(1-p));
	bar:SetHeight(h);
	bar:SetWidth(width);
	bar:SetTexCoord(0, 1, top, bottom );
	bar:SetPoint(point, texture, relative, gap_left, gap_bottom);
	bar:SetVertexColor(r, g, b, a);

	if(math.floor(p*100) == 0) then
		bar:Hide();
	else
		bar:Show();
	end
	
	if frame.targetchanged then
		starttime[vals] = p
		frame.targetchanged = nil
	end
end